package com.jushu.storbox.fragment;

import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.jushu.storbox.R;
import com.jushu.storbox.ShareDriveApplication;
import com.jushu.storbox.actions.Actions;
import com.jushu.storbox.adapter.MoveAdapter;
import com.jushu.storbox.entities.FileFolderInfo;
import com.jushu.storbox.service.ICloudDriveService;
import com.jushu.storbox.util.LogUtil;
import com.jushu.storbox.util.PublicTools;
import com.jushu.storbox.view.pulltorefresh.MyScrollView;
import com.huawei.sharedrive.sdk.android.exception.ClientException;

public class SharedFileFragment extends BaseFragment {

	/** ShareFragment TAG */
	public static final String TAG = "MoveFragment";
	/** activity */
	private Activity activity;
	/** activity handler */
	private Handler activityHandler;
	/** cloudriverService */
	private ICloudDriveService shareDriveService;
	/** move parent */
	private FileFolderInfo parentInfo;
	/** contentView */
	private View contentView;
	/** ListView */
	private ListView FileList_lv;
	/** canccle_btn */
	private List<FileFolderInfo> list;
	private int index = 0;
	private MyScrollView moveScr;
	private LinearLayout moveLL;
	private Actions actions;
	private String returnParent;
	private LinearLayout loading_ll;

	/**
	 * onAttach
	 * 
	 * @param activity
	 *            Activity instance
	 */
	@Override
	public void onAttach(Activity activity) {
		LogUtil.i(TAG, "onAttach Activity");
		this.activity = activity;
		setBack(false);
		if (null == getObj()) {
			System.exit(1);
		} else {
			Map<String, Object> mp = (Map<String, Object>) getObj();
			if (null != mp.get("activityhandler")) {
				activityHandler = (Handler) mp.get("activityhandler");
			} else {
				PublicTools.restartApp(activity);
			}
			if (null != mp.get("service")) {
				shareDriveService = (ICloudDriveService) mp.get("service");
			} else {
				PublicTools.restartApp(activity);
			}
			parentInfo = (FileFolderInfo) mp.get("move_entity");
			if (null != parentInfo) {
				returnParent = parentInfo.getName();
			}
			if (null != mp.get("index")) {
				index = (Integer) mp.get("index");
			}
			actions = new Actions(activity);
		}
		super.onAttach(activity);
	}

	/**
	 * onCreate in Fragment
	 * 
	 * @param savedInstanceState
	 *            Bundle
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		LogUtil.i(TAG, "onCreate()");
		super.onCreate(savedInstanceState);
	}

	/**
	 * onCreateView in Fragment
	 * 
	 * @param inflater
	 *            LayoutInflater
	 * @param container
	 *            ViewGroup
	 * @param savedInstanceState
	 *            Bundle
	 */
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		LogUtil.i(TAG, "onCreateView method");
		contentView = LayoutInflater.from(getActivity()).inflate(
				R.layout.window_move, null);
		initFindViewById();
		initData();
		initListenser();
		return contentView;
	}

	public void moveConfirm() {
		Intent intent = new Intent();
		if (null == parentInfo || null == parentInfo.getId()
				|| null == parentInfo.getName()) {
			intent.putExtra("fileParentID", "0");
			intent.putExtra("fileParentName", getString(R.string.my_cloud_drive));
		} else {
			intent.putExtra("fileParentID", parentInfo.getId());
			intent.putExtra("fileParentName", parentInfo.getName());
		}
		activity.setResult(1, intent); 
		activity.finish();
	}

	public void moveCancle() {
		// activityHandler.sendEmptyMessage(8);
		Intent intent = new Intent();
		activity.setResult(2, intent);
		activity.finish();
	}

	private void initListenser() {
	}

	private void initData() {
		if (View.GONE == loading_ll.getVisibility()) {
			loading_ll.setVisibility(View.VISIBLE);
		}
		moveScr.setLayout(moveLL);
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					// TODO Auto-generated method stub
					String folderId = "0";
					if (null != parentInfo) {
						folderId = parentInfo.getId();
					}
					list = getMoveFolderList(folderId, null);
					Message msg = new Message();
					msg.what = 0;
					msg.obj = list;
					handler.sendMessageDelayed(msg, 510);
				} catch (ClientException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}).start();
	}

	private void initFindViewById() {
		FileList_lv = (ListView) contentView.findViewById(R.id.FileList_lv);
		moveScr = (MyScrollView) contentView.findViewById(R.id.move_scrollview);
		moveLL = (LinearLayout) contentView.findViewById(R.id.move_layout);
		loading_ll = (LinearLayout) contentView.findViewById(R.id.loading_ll);
	}

	/**
	 * onActivityCreated in Fragment
	 * 
	 * @param savedInstanceState
	 *            Bundle
	 */
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		LogUtil.i(TAG, "onActivityCreated()");
		super.onActivityCreated(savedInstanceState);
	}

	/**
	 * onStart in Fragment
	 */
	@Override
	public void onStart() {
		LogUtil.i(TAG, "onStart()");
		super.onStart();
	}

	/**
	 * onResume in Fragment
	 */
	@Override
	public void onResume() {
		LogUtil.i(TAG, "onResume()");
		super.onResume();
	}

	/**
	 * onPause in Fragment
	 */
	@Override
	public void onPause() {
		LogUtil.i(TAG, "onResume()");
		super.onPause();
	}

	/**
	 * onSaveInstanceState in Fragment
	 * 
	 * @param savedInstanceState
	 *            Bundle
	 */
	@Override
	public void onSaveInstanceState(Bundle savedInstanceState) {
		LogUtil.i(TAG, "onSaveInstanceState()");
		super.onSaveInstanceState(savedInstanceState);
	}

	/**
	 * onStop in Fragment
	 */
	@Override
	public void onStop() {
		LogUtil.i(TAG, "onStop()");
		super.onStop();
	}

	/**
	 * onDestroy in Fragment
	 */
	@Override
	public void onDestroy() {
		LogUtil.i(TAG, "onDestroy()");
		super.onDestroy();
	}

	/**
	 * onDestroyView in Fragment
	 */
	@Override
	public void onDestroyView() {
		LogUtil.i(TAG, "onDestroyView()");
		super.onDestroyView();
	}

	/**
	 * onDetach in Fragment
	 */
	@Override
	public void onDetach() {
		LogUtil.i(TAG, "onDetach()");
		super.onDetach();
	}

	private void showMsgData() {
		setBack(true);
		if (View.VISIBLE == loading_ll.getVisibility()) {
			loading_ll.setVisibility(View.GONE);
		}
	}

	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 0:
				moveScr.removeAllItem();
				List<FileFolderInfo> list = (List<FileFolderInfo>) msg.obj;
				MoveAdapter adapter = new MoveAdapter(activity, handler, list);
				moveScr.setAdptor(adapter);
				moveScr.notifyAllItem();
				showMsgData();
				break;
			case 1:
				list = null;
				Message moveMsg = new Message();
				moveMsg.obj = msg.obj;
				moveMsg.what = 2;
				moveMsg.arg1 = 0;
				activityHandler.sendMessage(moveMsg);
				break;
			default:
				break;
			}
		};
	};

	private List<FileFolderInfo> getMoveFolderList(String folderId,
			String exception) throws ClientException {
		List<FileFolderInfo> moveList = null;
		if (null != shareDriveService) {
			String ownerId = ShareDriveApplication.getInstance().getWnerID();
			moveList = shareDriveService.getMoveFolderListDB(ownerId,folderId,
					exception);
			// for (int i = 0; i < moveList.size(); i++) {
			// Log.d("dsdsd22", "name:" + moveList.get(i).getName());
			// }
		}
		return moveList;

	}

	@Override
	public void refreshListFromServer() {
		// TODO Auto-generated method stub
	}

	@Override
	public String getParent() {
		// TODO Auto-generated method stub
		return returnParent;
	}

	@Override
	public void onDataLoaded() {
		// TODO Auto-generated method stub
	}
}
